home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: "Paul D. DeRocco" <pderocco@ix.netcom.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Exceptions and operator new. Help
- Date: Sat, 17 Feb 1996 04:30:46 -0500
- Organization: Netcom
- Message-ID: <3125A046.202E@ix.netcom.com>
- References: <4fu14a$2vg@news.tamu.edu>
- NNTP-Posting-Host: ix-bst-ma1-19.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-NETCOM-Date: Sat Feb 17 1:37:12 AM PST 1996
- X-Mailer: Mozilla 2.0b6a (Win95; I)
-
- You don't have to do that. Allocation and construction are treated as a
- unit by the compiler. If operator new throws an exception, nothing will
- happen. If the constructor throws an exception, operator delete will
- automatically be called before the exception is propagated. If there are
- class members that have destructors and you throw an exception after
- some of them have been successfully constructed, the compiler will
- ensure that the constructed ones will be destroyed and the unconstructed
- ones won't. And if you're dealing with derived classes, an exception
- that is thrown by a derived class constructor will call the destructors
- for all the base and less-derived classes.
-
- In other words, it works the way it "should."
-
- By the way, if you don't need to handle xalloc locally, but m_pVarList
- is a global that needs to be nullified in case the allocation fails,
- simply nullify it first:
-
- m_pVarList = NULL;
- m_pVarList = new char[10];
-
- If the allocation throws an exception, it will never return, so the
- second store into m_pVarList won't happen.
-
- --
-
- Ciao,
- Paul D. DeRocco
-